package com.xmall.sys.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmall.define.base.ResultCode;
import com.xmall.define.dto.sys.DictDto;
import com.xmall.exception.XmallException;
import com.xmall.sys.define.dto.dict.DictDo;
import com.xmall.sys.define.request.dict.DictListPageQueryRequest;
import com.xmall.sys.define.request.dict.DictUpdateRequest;
import com.xmall.sys.entity.Dict;
import com.xmall.sys.entity.DictValue;
import com.xmall.sys.mapper.DictMapper;
import com.xmall.sys.service.IDictService;
import com.xmall.sys.service.IDictValueService;
import com.xmall.sys.utils.ConvertUtils;
import com.xmall.utils.XmallUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @author xmall
 * @since 2023-12-05
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements IDictService {

    @Resource
    private IDictValueService dictValueService;

    /**
     * 字典详情
     */
    @Override
    public DictDto info(Long id) {
        DictDo dictDo = this.baseMapper.info(id);
        XmallUtils.notNull(dictDo);
        return ConvertUtils.DictModule.INSTANCE.doToDto(dictDo);
    }

    /**
     * 列表
     */
    @Override
    public List<DictDto> dictList() {
        return ConvertUtils.DictModule.INSTANCE.toDtoList(this.baseMapper.list());
    }

    /**
     * 分页列表
     */
    @Override
    public Page<DictDto> listPage(DictListPageQueryRequest request) {
        Page<DictDto> result = new Page<>();
        Page<DictDo> page = this.baseMapper.listPage(new Page(request.getPage(), request.getPageSize()), ConvertUtils.DictModule.INSTANCE.toQueryParams(request));

        BeanUtils.copyProperties(page, result);
        result.setRecords(ConvertUtils.DictModule.INSTANCE.toDtoList(page.getRecords()));
        return result;
    }

    /**
     * 新增
     */
    @Override
    public void saveEntity(DictUpdateRequest request) {
        long count = this.count(new QueryWrapper<Dict>().eq("code", request.getCode()));
        if(count > 0) {
            throw new XmallException(ResultCode.SYS_DICT_CODE_EXIST);
        }
        this.save(ConvertUtils.DictModule.INSTANCE.toEntity(request));
    }

    /**
     * 编辑
     */
    @Override
    public void updateEntity(DictUpdateRequest request) {
        Dict entity = this.getById(request.getId());
        XmallUtils.notNull(entity);

        Dict updateEntity = new Dict();
        updateEntity.setId(request.getId());
        updateEntity.setName(request.getName());
        this.updateById(updateEntity);
    }

    /**
     * 删除
     */
    @Transactional
    @Override
    public void deleteEntity(Long id) {
        this.removeById(id);
        dictValueService.remove(new QueryWrapper<DictValue>().eq("dict_id", id));
    }
}
